package com.example.demo.domain;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.math.BigDecimal;


@Component
public class CartItem implements Serializable {
    private static final long serialVersionUID = 6620528781626504362L;

    @Getter@Setter private Item item;

    @Getter@Setter private int quantity;

    @Getter@Setter private boolean inStock;

    @Getter@Setter private BigDecimal total;

    public void incrementQuantity() {
        quantity++;
        calculateTotal();
    }

    public void calculateTotal() {
        if (item != null && item.getListPrice() != null) {
            total = item.getListPrice().multiply(new BigDecimal(quantity));
        } else {
            total = null;
        }
    }
}
